import axios from 'axios'
import qs from 'qs'
import env from '@/assets/env'
import res from '@/assets/js/result'
// import {
//   Message
// } from 'element-ui'

let baseAxios = axios.create({
  baseURL: env.URL,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

baseAxios.getUrl = (url, data) => {
  return new Promise(resolve => {
    let param = '';
    if (data) {
      param = '?' + qs.stringify(data);
      url = url + param;
    }
    baseAxios.get(url).then(response => {
      const data = response.data;
      resolve(res.success(data));
    }).catch(err => {
      resolve(res.err('服务器返回错误', 500, err))
    })
  })
}

baseAxios.postUrl = (url, data) => {
  let headers = {};
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
  return new Promise(resolve => {
    baseAxios.post(url, data).then(response => {
      let retData = response.data;

      resolve(retData)
    }, error => {
      console.warn(error);
      resolve(res.err('错误', 500, error));
    })
  })
}

export default baseAxios;
